﻿#include "stdafx.h"
#include "UIOpt.h"
#include "StdioFileEx.h"
#include "GameAppDlg.h"
#include "resource.h"

CUIOpt::CUIOpt()
{
}


CUIOpt::~CUIOpt()
{
}


//导入账号到列表
void CUIOpt::ImoprtCode(CListCtrl* pListInfo)
{
	CString strFileName, strPathName;
	pListInfo->DeleteAllItems();
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, _T("文本文件(*.txt)|*.txt||"));
	if (dlg.DoModal() == IDOK)
	{
		strFileName = dlg.GetPathName();
		CStdioFileEx sFile;

		CStringArray ArrAcountInfo;
		if (!strFileName.IsEmpty())
		{
			sFile.Open(strFileName, CFile::modeRead);
			CString strLine;
			while (sFile.ReadString(strLine))
			{
				CString strTemp = strLine;
				int len = strLine.GetLength();
				if (len > 100)
				{
					sFile.Close();
					return;
				}
				//zhanghao-mima-daqu
				strLine.Replace(_T("----"), _T("-"));
				strLine.Replace(_T("---"), _T("-"));
				strLine.Replace(_T("--"), _T("-"));

				StringSplit(strLine, ArrAcountInfo, '-');

				int	iBegin = pListInfo->GetItemCount();

				int iRow = pListInfo->InsertItem(iBegin, NumberToString(iBegin));
				pListInfo->SetItemText(iRow, 1, ArrAcountInfo[0]);//瑯뵀
				pListInfo->SetItemText(iRow, 2, ArrAcountInfo[1]);//쵱쯤
				pListInfo->SetItemText(iRow, 3, ArrAcountInfo[2]);//댕혐
				pListInfo->SetItemText(iRow, 4, ArrAcountInfo[3]);//댕혐
				CString strLineCode = L"0123456789";
				int nCount = ArrAcountInfo.GetCount();
				int nLen = 0;
				if (nCount <= 6) {
					//if (strLineCode.Find(ArrAcountInfo[4]) > 0) {//存在子账号
					//	pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
					//}
					//else {
					//	pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
					//}
					if (nCount == 5) {
						pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
					}
					if (nCount == 6) {
						nLen = ArrAcountInfo[5].GetLength();
						CString cl = ArrAcountInfo[5];
						int nnn = ArrAcountInfo[5].Find(L"i");
						//是否是扎昆角色
						if (ArrAcountInfo[5].Find(L"$") >= 0) {
							pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
							pListInfo->SetItemText(iRow, 10, ArrAcountInfo[5].Mid(1, nLen - 1));
						}
						else {
							pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
							pListInfo->SetItemText(iRow, 12, ArrAcountInfo[5]);
						}
					}

				}
				else if (ArrAcountInfo.GetCount() == 7) {
					pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
					pListInfo->SetItemText(iRow, 12, ArrAcountInfo[5]);
					nLen = ArrAcountInfo[6].GetLength();
					pListInfo->SetItemText(iRow, 10, ArrAcountInfo[6].Mid(1, nLen - 1));


				}

				pListInfo->SetItemText(iRow, 6, L"未完成");//댕혐
				if (!ArrAcountInfo[0].IsEmpty() && !ArrAcountInfo[1].IsEmpty())//&& !ArrAcountInfo[2].IsEmpty())
				{

					g_AccountFile.SetString(NumberToString(iRow + 1), L"code", ArrAcountInfo[0]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"password", ArrAcountInfo[1]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"twocode", ArrAcountInfo[2]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"server", ArrAcountInfo[3]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"RoleNum", L"0");
					if (ArrAcountInfo.GetCount() <= 6) {
						CString codeLine = ArrAcountInfo[4];
						if (ArrAcountInfo.GetCount() == 5) {
							g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);
						}

						if (nCount == 6) {
							nLen = ArrAcountInfo[5].GetLength();
							if (ArrAcountInfo[5].Find(L"$") >= 0) {
								g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[5].Mid(1, nLen - 1));
								g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);

							}
							else {
								g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
								g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

							}
						}

					}if (ArrAcountInfo.GetCount() == 7) {
						nLen = ArrAcountInfo[6].GetLength();
						g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[6].Mid(1, nLen - 1));
						g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
						g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

					}


					g_AccountFile.SetString(NumberToString(iRow + 1), L"state", L"0");

				}

			}
			sFile.Close();
		}
	}

}

void CUIOpt::ImportCode(CListCtrl* pListInfo,CString strPath)
{
	pListInfo->DeleteAllItems();
	CStdioFileEx sFile;
	CStringArray ArrAcountInfo;
	if (!strPath.IsEmpty())
	{
		sFile.Open(strPath, CFile::modeRead);
		CString strLine;
		while (sFile.ReadString(strLine))
		{
			CString strTemp = strLine;
			int len = strLine.GetLength();
			if (len > 100)
			{
				sFile.Close();
				return;
			}
			//zhanghao-mima-daqu
			strLine.Replace(_T("----"), _T("-"));
			strLine.Replace(_T("---"), _T("-"));
			strLine.Replace(_T("--"), _T("-"));

			StringSplit(strLine, ArrAcountInfo, '-');

			int	iBegin = pListInfo->GetItemCount();

			int iRow = pListInfo->InsertItem(iBegin, NumberToString(iBegin));
			pListInfo->SetItemText(iRow, 1, ArrAcountInfo[0]);//瑯뵀
			pListInfo->SetItemText(iRow, 2, ArrAcountInfo[1]);//쵱쯤
			pListInfo->SetItemText(iRow, 3, ArrAcountInfo[2]);//댕혐
			pListInfo->SetItemText(iRow, 4, ArrAcountInfo[3]);//댕혐
			CString strLineCode = L"0123456789";
			int nCount = ArrAcountInfo.GetCount();
			int nLen = 0;
			if (nCount <= 6) {
				//if (strLineCode.Find(ArrAcountInfo[4]) > 0) {//存在子账号
				//	pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
				//}
				//else {
				//	pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
				//}
				if (nCount == 5) {
					pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
				}
				if (nCount == 6) {
					nLen = ArrAcountInfo[5].GetLength();
					CString cl = ArrAcountInfo[5];
					int nnn = ArrAcountInfo[5].Find(L"i");
					//是否是扎昆角色
					if (ArrAcountInfo[5].Find(L"$") >= 0) {
						pListInfo->SetItemText(iRow, 12, ArrAcountInfo[4]);
						pListInfo->SetItemText(iRow, 10, ArrAcountInfo[5].Mid(1, nLen - 1));
					}
					else {
						pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
						pListInfo->SetItemText(iRow, 12, ArrAcountInfo[5]);
					}
				}

			}
			else if (ArrAcountInfo.GetCount() == 7) {
				pListInfo->SetItemText(iRow, 11, ArrAcountInfo[4]);
				pListInfo->SetItemText(iRow, 12, ArrAcountInfo[5]);
				nLen = ArrAcountInfo[6].GetLength();
				pListInfo->SetItemText(iRow, 10, ArrAcountInfo[6].Mid(1, nLen - 1));


			}

			pListInfo->SetItemText(iRow, 6, L"未完成");//댕혐
			if (!ArrAcountInfo[0].IsEmpty() && !ArrAcountInfo[1].IsEmpty())//&& !ArrAcountInfo[2].IsEmpty())
			{

				g_AccountFile.SetString(NumberToString(iRow + 1), L"code", ArrAcountInfo[0]);
				g_AccountFile.SetString(NumberToString(iRow + 1), L"password", ArrAcountInfo[1]);
				g_AccountFile.SetString(NumberToString(iRow + 1), L"twocode", ArrAcountInfo[2]);
				g_AccountFile.SetString(NumberToString(iRow + 1), L"server", ArrAcountInfo[3]);
				g_AccountFile.SetString(NumberToString(iRow + 1), L"RoleNum", L"0");
				if (ArrAcountInfo.GetCount() <= 6) {
					CString codeLine = ArrAcountInfo[4];
					if (ArrAcountInfo.GetCount() == 5) {
						g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);
					}

					if (nCount == 6) {
						nLen = ArrAcountInfo[5].GetLength();
						if (ArrAcountInfo[5].Find(L"$") >= 0) {
							g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[5].Mid(1, nLen - 1));
							g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[4]);

						}
						else {
							g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
							g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

						}
					}

				}if (ArrAcountInfo.GetCount() == 7) {
					nLen = ArrAcountInfo[6].GetLength();
					g_AccountFile.SetString(NumberToString(iRow + 1), L"ZhaNum", ArrAcountInfo[6].Mid(1, nLen - 1));
					g_AccountFile.SetString(NumberToString(iRow + 1), L"childCode", ArrAcountInfo[4]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"CodeLine", ArrAcountInfo[5]);

				}


				g_AccountFile.SetString(NumberToString(iRow + 1), L"state", L"0");

			}

		}
		sFile.Close();
	}
}




//初始化数据
void CUIOpt::InitConfig(CListCtrl * pListInfo)
{
	pListInfo->DeleteAllItems();
	CStringArray Sections;
	int iCount = g_AccountFile.GetSections(Sections);
	if (iCount <= 0)
	{
		return;
	}
	CString str; //获取系统时间 　　
	CTime tm; tm = CTime::GetCurrentTime();
	str = tm.Format("%H");
	CString sDate = g_IniFile.GetString(L"Set", L"AppDate");
	if (_wtoi(str) >= 17 && _wtoi(str) < 23) {
		
		g_main_cwnd->m_ZhaKun.SetCheck(0);
	}
	else {
		g_main_cwnd->m_ZhaKun.SetCheck(1);
	}
	for (int i = 0; i < iCount; i++)
	{
		int ItemCount = pListInfo->InsertItem(pListInfo->GetItemCount(), NumberToString(i));
		pListInfo->SetItemText(ItemCount, 1, g_AccountFile.GetString(NumberToString(i + 1), _T("code")));
		pListInfo->SetItemText(ItemCount, 2, g_AccountFile.GetString(NumberToString(i + 1), _T("password")));
		pListInfo->SetItemText(ItemCount, 3, g_AccountFile.GetString(NumberToString(i + 1), _T("twocode")));
		pListInfo->SetItemText(ItemCount, 4, g_AccountFile.GetString(NumberToString(i + 1), _T("server")));
		pListInfo->SetItemText(ItemCount, 6, arrState[g_AccountFile.GetInt(NumberToString(i + 1), _T("state"))]);
		pListInfo->SetItemText(ItemCount, 8, g_AccountFile.GetString(NumberToString(i + 1), _T("bbjb")));
		pListInfo->SetItemText(ItemCount, 11, g_AccountFile.GetString(NumberToString(i + 1), _T("childCode")));
		pListInfo->SetItemText(ItemCount, 12, g_AccountFile.GetString(NumberToString(i + 1), _T("CodeLine")));
		pListInfo->SetItemText(ItemCount, 10, g_AccountFile.GetString(NumberToString(i + 1), _T("ZhaNum")));
		pListInfo->SetItemText(ItemCount, 7, g_AccountFile.GetString(NumberToString(i + 1), _T("RoleNum")));
	}
}

void CUIOpt::SaveIni()
{
	
	CString str;
	g_main_cwnd->GetDlgItemTextW(IDC_EDIT1, str);
	AfxGetApp()->WriteProfileStringW(_T("cfg"), L"PATH", str);
	CString strList;
	for (size_t i = 0; i < g_main_cwnd->m_listChangeLine.GetCount(); i++)
	{
		CString str;
		g_main_cwnd->m_listChangeLine.GetText(i, str);
		if (i == g_main_cwnd->m_listChangeLine.GetCount() - 1) {
			strList += str;
		}
		else {

			strList += str + L",";
		}
	}
	AfxGetApp()->WriteProfileStringW(_T("cfg"), L"LineList", strList);

}
